﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using SystemHYS.API.Application.command.RawmaterialtestingCommand;
using SystemHYS.Domian;
using SystemHYS.Infrstructre;
using SystemHYS.Infrstructre.Imp;
using SystemHYS.Infrstructre.Interface;
namespace SystemHYS.API.Application.commandler.RawmaterialtestingCommandler
{
    
    public class RawmaterialtestingCreateCommandler : IRequestHandler<RawmaterialtestingCreateCommand, int>
    {
        IRawmaterialtestingGualityProjectRepository rawmaterialtestingGualityProjectRepository;
        IRawmaterialtestingRepository rawmaterialtestingRepository;
        public SystemCoretext db;
        public RawmaterialtestingCreateCommandler(IRawmaterialtestingGualityProjectRepository rawmaterialtestingGualityProjectRepository, IRawmaterialtestingRepository rawmaterialtestingRepository, SystemCoretext db)
        {
            this.rawmaterialtestingGualityProjectRepository = rawmaterialtestingGualityProjectRepository;
            this.rawmaterialtestingRepository = rawmaterialtestingRepository;
            this.db = db;
        }

        public async Task<int> Handle(RawmaterialtestingCreateCommand request, CancellationToken cancellationToken)
        {
            using (var tran = await db.Database.BeginTransactionAsync(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    var list = new Rawmaterialtesting()
                    {
                        RawMaterialtestingId = 0,
                        qualityNumber = request.qualityNumber,
                        qualityName = request.qualityName,
                        qualityDate = request.qualityDate.Value.ToUniversalTime(),
                        qualityPerson = request.qualityPerson,
                        vendor = request.vendor,
                        ingredientNumber =  request.ingredientNumber,
                        ingredientName=request.ingredientName,
                        unit=request.unit,
                        specification=request.specification,
                        rawMaterialBatchNumber=request.rawMaterialBatchNumber,
                        size=request.size,
                        typeOfQC=request.typeOfQC,
                        gualitySchemeName=request.gualitySchemeName,
                        state=request.state,
                        AddName = "李明",
                        AddTime = DateTime.Now.ToUniversalTime()
                    };
                    await rawmaterialtestingRepository.AddAsyn(list);
                    var id = list.RawMaterialtestingId;
                    foreach (var item in request.GualityProjectList)
                    {
                        await rawmaterialtestingGualityProjectRepository.AddAsyn(new RawmaterialtestingGualityProject()
                        {
                            RawMaterialtestingId = id,
                            gualityProjectId = item.gualityProjectId
                        });
                    }
                    await tran.CommitAsync();
                    return 1;
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
    }
}
